<?php
/**
* @package   auto_occasion
* @subpackage essais
* @author    your name
* @copyright 2011 your name
* @link      http://www.yourwebsite.undefined
* @license    All rights reserved
*/

class FoDossierCtrl extends jController {
    /**
    * Page dossier - front
    */
    function index() {

        $rep = $this->getResponse('htmlFo');
        $rep->menusActifs = frontHtmlResponse::MENU_ESSAI;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ESSAI) ;

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/sliderkit.css');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.easing.1.3.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.sliderkit.1.9.1.pack.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');
        
        jClasses::inc('commun~common');
        jClasses::inc('essais~dossierChapitreSrv') ;
        jClasses::inc('commun~toolsFile');


        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ESSAIS);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---

        $idDossier = $this->intParam('id', 0);

        $dossier   = ($idDossier) ? common::chargeParId($idDossier, 'ao_dossier')  : common::init('ao_dossier');
        $chapitre  = ($idDossier) ? dossierChapitreSrv::getByDossierId($idDossier) : array();

        $tempC = array();


        if (sizeof($chapitre)) {
            foreach ($chapitre as $itemChap) {
                if (strpos($itemChap->chap_content, '.txt')) {

                    $path = JELIX_APP_WWW_PATH . PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $itemChap->dossier_id . '/' . $itemChap->chap_content;
                    $itemChap->chap_content = toolsFile::get($path);
                }

                if ($itemChap->chap_img && strpos($itemChap->chap_img, ',')) {
                    $itemChap->chap_img = explode(',', $itemChap->chap_img);
                    $t = array();
                    foreach ($itemChap->chap_img as $img) {
                        if (file_exists(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $img)) {
                            $t[] = PATH_ESSAI_IMG . $img;
                        }
                    }

                    $itemChap->chap_img = $t;

                } elseif (file_exists(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $itemChap->chap_img)) {
                    $itemChap->chap_img = PATH_ESSAI_IMG . $itemChap->chap_img;
                }
                $tempC[] = $itemChap;
            }
        }
		
		//preparation du $dossier->dossier_content
        if ($dossier->dossier_content) {
            $content		= $dossier->dossier_content ;
            $filename		= JELIX_APP_WWW_PATH . PATH_ESSAI_CHAPEAU . $content ;
            if (strpos($content, ".txt") !== false) {
                //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                jClasses::inc('commun~toolsFile') ;
                $dossier->dossier_content = toolsFile::get($filename) ;
            }
        }


        //preparation du $dossier->dossier_img
        if ($idDossier) {
            $photo = $const = array();
            if (strpos($dossier->dossier_img, ',') !== false) {
                $tphotoChap = explode(',', $dossier->dossier_img);
                foreach ($tphotoChap as $item) {
                    $withoutConst = 0;
                    if ($item && is_file(JELIX_APP_WWW_PATH . $item)) {
                        $withoutConst = 1;
                        $photo[] = $item;
                    } else {
                        $photo[] = PATH_ESSAI_IMG . $item;
                    }
                    $const[] = $withoutConst;
                }
            } else if ($dossier->dossier_img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $dossier->dossier_img)) {
                $photo[] = PATH_ESSAI_IMG . $dossier->dossier_img;
                $const[] = 0;
            } else if ($dossier->dossier_img && is_file(JELIX_APP_WWW_PATH . $dossier->dossier_img)) {
                $photo[] = $dossier->dossier_img;
                $const[] = 1;
            }
            $dossier->image        = $photo;
            $dossier->withoutConst = $const;
        }

        $rep->bodyTpl	= 'essais~FoDossier';
        $rep->body->assign("dossier", $dossier);
        $rep->body->assign("jelix_app_www_path", JELIX_APP_WWW_PATH . PATH_ESSAI_CHAPEAU);
        $rep->body->assign("chapitre", $tempC);
        $rep->body->assignZone('oZoneRightEssai', 'essais~FoEssaiRight');

        return $rep;
    }


     /**
    * Page dossier - front : les chapitres
    */
    function chapitre() {

        jClasses::inc('commun~common');
        jClasses::inc('essais~chapitreSrv');
		jClasses::inc('publicite~pubSrv');
		jClasses::inc('commun~parametre');
        jClasses::inc('commun~toolsFile');
        jClasses::inc('essais~dossierChapitreSrv') ;



        $rep = $this->getResponse('htmlFo');
        $rep->menusActifs = frontHtmlResponse::MENU_ESSAI;
        $rep->breadCrumb  = array(frontHtmlResponse::MENU_ACCUEIL, frontHtmlResponse::MENU_ESSAI) ;

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/css/sliderkit.css');
        
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/carousel_custom.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.idTabs.min.js');
//        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.galleriffic.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.easing.1.3.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/jquery.sliderkit.1.9.1.pack.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/essai.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/front/js/pub.js');


        //xml pour le header : title, desc, keywords
        $headerContent = common::readXml(PATH_XML_ESSAIS_CHAPITRES);
        // change le header de la page en cours
        $rep->title					=	isset($headerContent->title)?$headerContent->title:'' ;
		$headerContent->keywords	=	isset($headerContent->keywords)?$headerContent->keywords:'' ;
		$headerContent->desc		=	isset($headerContent->desc)?$headerContent->desc:'' ;
		$keywords					= "<meta name='keywords' content='" . $headerContent->keywords . "'>" ;
		$description				= "<meta name='Description' content='" . $headerContent->desc. "'>" ;
        $rep->addHeadContent($keywords);
        $rep->addHeadContent($description);
        // ---
        $id = $this->intParam('chap', 0);
        $chapitre   = ($id) ? common::chargeParId($id, 'ao_dossier_chapitre')  : common::init('ao_dossier_chapitre');
		
        $photo = array();
        if (strpos($chapitre->chap_img, ',') !== false) {
            $tphotoChap = explode(',', $chapitre->chap_img);
            foreach ($tphotoChap as $item) {
                if (is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $item)) {
                    $photo[] = PATH_ESSAI_IMG .'/'. $item;
                } else if (is_file(JELIX_APP_WWW_PATH . $item)) {
                    $photo[] = $item;
                }
            }
        } else if ($chapitre->chap_img && is_file(JELIX_APP_WWW_PATH . PATH_ESSAI_IMG . $chapitre->chap_img)) {
            $photo[] = PATH_ESSAI_IMG . $chapitre->chap_img;
        } else if ($chapitre->chap_img && is_file(JELIX_APP_WWW_PATH . $chapitre->chap_img)) {
            $photo[] = $chapitre->chap_img;
        }
		
        if (strpos($chapitre->chap_content, '.txt')) {

            $path = JELIX_APP_WWW_PATH.'/'. PATH_ESSAI_SOUSRUBRIQUE . 'guide' . $chapitre->dossier_id . '/' . $chapitre->chap_content;
            $chapitre->chap_content = toolsFile::get($path);
        }
        
        // navigation sur les chapitres du dossier
        $currentChap = $id;
        $currentDoss = $this->intParam('dossId', 0);
        $tempChapIds = array();
        $chapitre_    = ($currentDoss) ? dossierChapitreSrv::getByDossierId($currentDoss) : array();
        if (sizeof($chapitre_)) {
            foreach ($chapitre_ as $itemChap) {
                if ($itemChap) {
                    $tempChapIds[] = $itemChap->chap_id;
                }
            }
        }
        // next et previous chapter
        $navigation = new stdClass();

        if ($currentChap && sizeof($tempChapIds)) {
            $currentKey    = array_keys($tempChapIds, $currentChap);

            $navigation->suiv       = (isset($tempChapIds[$currentKey[0] + 1 ])) ? $tempChapIds[$currentKey[0] + 1 ] : 0;
            $navigation->prec       = (isset($tempChapIds[$currentKey[0] - 1 ])) ? $tempChapIds[$currentKey[0] - 1 ] : 0;
            $navigation->suivMod    = common::chargeParId($tempChapIds[$currentKey[0] + 1] , 'ao_dossier_chapitre')->chap_name;
            $navigation->precMod    = common::chargeParId($tempChapIds[$currentKey[0] - 1] , 'ao_dossier_chapitre')->chap_name;

        } else {
            $navigation->suiv    = $navigation->prec    = 0;
            $navigation->precMod = $navigation->suivMod = '';
        }
        $rep->bodyTpl = 'essais~FoDossierChapitre';
        $rep->body->assign("chapitre", $chapitre);
        $rep->body->assign("photo", $photo);
        $rep->body->assign("navigation", $navigation);
        $rep->body->assign("currentDoss", $currentDoss);

         //pubHidden et rightContent
        $paramPub = array('p1'=>PUB_RIGHT_1_ID,'p2'=>PUB_RIGHT_2_ID,'p3'=>PUB_RIGHT_3_ID,'p4'=>PUB_RIGHT_4_ID,'p5'=>PUB_RIGHT_5_ID,'p6'=>PUB_RIGHT_6_ID);
        $rep->body->assignZone('oZonePubHidden', 'commun~FoPubHidden',$paramPub);
        
        $paramRight = array('ad'=>1,'no_need_cc2'=>0);
        $rep->body->assignZone('oZoneRightContent', 'commun~FoRightContent',$paramRight);
        //---

        return $rep;
    }


}